﻿using Infrastructure.WebApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Infrastructure.Utils
{
    public class JsonSettings
    {
        #region json序列化配置
        /// <summary>
        /// json序列化配置 一
        /// 小驼峰、"yyyy/MM/dd HH:mm:ss"（yyyy-MM-dd形式ios不兼容 ）
        /// </summary>
        public static JsonSerializerSettings Default()
        {
            var setting = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy/MM/dd HH:mm:ss"
            };
            setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
            return setting;
        }
        /// <summary>
        /// json序列化配置 二
        /// 小驼峰、"yyyy/MM/dd HH:mm:ss"，处理HandleStringNullToEmpty
        /// </summary>
        public static JsonSerializerSettings JsonSettingHandleStringNullToEmpty()
        {
            var setting = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy/MM/dd HH:mm:ss"
            };
            setting.ContractResolver = new HandleStringNullToEmptyContractResolver();
            return setting;
        }
        /// <summary>
        /// json序列化配置 三
        /// 小驼峰、"yyyy/MM/dd HH:mm:ss"，序列化忽略NUll值属性
        /// </summary>
        public static JsonSerializerSettings JsonSettingIgnoreNullValue()
        {
            var setting = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy/MM/dd HH:mm:ss",
                NullValueHandling = NullValueHandling.Ignore
            };
            return setting;
        }
        /// <summary>
        /// json序列化配置 三
        /// 小驼峰、"yyyy/MM/dd HH:mm:ss"，序列化忽略NUll值属性
        /// </summary>
        public static JsonSerializerSettings JsonSettingIndented()
        {
            var setting = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy/MM/dd HH:mm:ss",
                Formatting = Formatting.Indented,
            };
            return setting;
        }
        #endregion
    }
}
